package com.mqttinsight.ui.form.panel;

import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;
import com.mqttinsight.exception.VerificationException;
import com.mqttinsight.mqtt.SecureSetting;
import com.mqttinsight.util.LangUtil;
import com.mqttinsight.util.Validator;

import javax.swing.*;

public class ServerAndClientKeystorePanel extends SecurePanel {
    private JPanel rootPanel;
    private JLabel serverKeystoreFileLabel;
    private JTextField serverKeystoreFileField;
    private JLabel serverKeystorePasswordLabel;
    private JPasswordField serverKeystorePasswordField;
    private JLabel clientKeystoreFileLabel;
    private JTextField clientKeystoreFileField;
    private JLabel clientKeystorePasswordLabel;
    private JPasswordField clientKeystorePasswordField;
    private JLabel clientKeyPasswordLabel;
    private JPasswordField clientKeyPasswordField;

    public ServerAndClientKeystorePanel() {
        super();
        $$$setupUI$$$();
        applyLanguage();
    }

    @Override
    public JPanel getRootPanel() {
        return rootPanel;
    }

    @Override
    public void applyLanguage() {
        serverKeystoreFileLabel.setText(LangUtil.getString("CaKeystoreFile"));
        clientKeyPasswordLabel.setText(LangUtil.getString("ClientKeyPassword"));
        clientKeystoreFileLabel.setText(LangUtil.getString("ClientKeystoreFile"));
        clientKeystorePasswordLabel.setText(LangUtil.getString("ClientKeystorePassword"));
        serverKeystorePasswordLabel.setText(LangUtil.getString("CaKeystorePassword"));
    }

    @Override
    public void changeFieldsEnable(boolean enabled) {
        serverKeystoreFileField.setEnabled(enabled);
        serverKeystorePasswordField.setEnabled(enabled);
        clientKeystoreFileField.setEnabled(enabled);
        clientKeystorePasswordField.setEnabled(enabled);
        clientKeyPasswordField.setEnabled(enabled);
    }

    @Override
    public void applySetting(SecureSetting setting) {
        if (setting != null) {
            serverKeystoreFileField.setText(setting.getServerKeyStoreFile());
            serverKeystorePasswordField.setText(setting.getServerKeyStorePassword());
            clientKeystoreFileField.setText(setting.getClientKeyStoreFile());
            clientKeystorePasswordField.setText(setting.getClientKeyStorePassword());
            clientKeyPasswordField.setText(setting.getClientKeyPassword());
        }
    }

    @Override
    public void resetFields() {
        serverKeystoreFileField.setText("");
        serverKeystorePasswordField.setText("");
        clientKeystoreFileField.setText("");
        clientKeystorePasswordField.setText("");
        clientKeyPasswordField.setText("");
    }

    @Override
    public void verifyFields() throws VerificationException {
        Validator.notEmpty(serverKeystoreFileField, () -> LangUtil.format("FieldRequiredValidation", serverKeystoreFileLabel.getText()));
        Validator.notEmpty(serverKeystorePasswordField, () -> LangUtil.format("FieldRequiredValidation", serverKeystorePasswordLabel.getText()));
        Validator.notEmpty(clientKeystoreFileField, () -> LangUtil.format("FieldRequiredValidation", clientKeystoreFileLabel.getText()));
        Validator.notEmpty(clientKeystorePasswordField, () -> LangUtil.format("FieldRequiredValidation", clientKeystorePasswordLabel.getText()));
        Validator.notEmpty(clientKeyPasswordField, () -> LangUtil.format("FieldRequiredValidation", clientKeyPasswordLabel.getText()));
    }

    @Override
    public SecureSetting getSetting() throws VerificationException {
        SecureSetting setting = new SecureSetting();
        setting.setServerKeyStoreFile(serverKeystoreFileField.getText());
        setting.setServerKeyStorePassword(String.valueOf(serverKeystorePasswordField.getPassword()));
        setting.setClientKeyStoreFile(clientKeystoreFileField.getText());
        setting.setClientKeyStorePassword(String.valueOf(clientKeystorePasswordField.getPassword()));
        setting.setClientKeyPassword(String.valueOf(clientKeyPasswordField.getPassword()));
        return setting;
    }

    /**
     * Method generated by IntelliJ IDEA GUI Designer
     * >>> IMPORTANT!! <<<
     * DO NOT edit this method OR call it in your code!
     *
     * @noinspection ALL
     */
    private void $$$setupUI$$$() {
        rootPanel = new JPanel();
        rootPanel.setLayout(new FormLayout("fill:170px:noGrow,left:4dlu:noGrow,fill:d:grow", "center:d:noGrow,top:4dlu:noGrow,center:max(d;4px):noGrow,top:4dlu:noGrow,center:max(d;4px):noGrow,top:4dlu:noGrow,center:max(d;4px):noGrow,top:4dlu:noGrow,center:max(d;4px):noGrow"));
        serverKeystoreFileLabel = new JLabel();
        serverKeystoreFileLabel.setText("CA keystore file");
        CellConstraints cc = new CellConstraints();
        rootPanel.add(serverKeystoreFileLabel, cc.xy(1, 1));
        serverKeystoreFileField = new JTextField();
        rootPanel.add(serverKeystoreFileField, cc.xy(3, 1, CellConstraints.FILL, CellConstraints.DEFAULT));
        clientKeyPasswordLabel = new JLabel();
        clientKeyPasswordLabel.setText("Client key password");
        rootPanel.add(clientKeyPasswordLabel, cc.xy(1, 9));
        clientKeyPasswordField = new JPasswordField();
        rootPanel.add(clientKeyPasswordField, cc.xy(3, 9, CellConstraints.FILL, CellConstraints.DEFAULT));
        clientKeystoreFileLabel = new JLabel();
        clientKeystoreFileLabel.setText("Client keystore file");
        rootPanel.add(clientKeystoreFileLabel, cc.xy(1, 5));
        clientKeystorePasswordLabel = new JLabel();
        clientKeystorePasswordLabel.setText("Client keystore password");
        rootPanel.add(clientKeystorePasswordLabel, cc.xy(1, 7));
        clientKeystoreFileField = new JTextField();
        rootPanel.add(clientKeystoreFileField, cc.xy(3, 5, CellConstraints.FILL, CellConstraints.DEFAULT));
        serverKeystorePasswordLabel = new JLabel();
        serverKeystorePasswordLabel.setText("CA keystore password");
        rootPanel.add(serverKeystorePasswordLabel, cc.xy(1, 3));
        serverKeystorePasswordField = new JPasswordField();
        rootPanel.add(serverKeystorePasswordField, cc.xy(3, 3, CellConstraints.FILL, CellConstraints.DEFAULT));
        clientKeystorePasswordField = new JPasswordField();
        rootPanel.add(clientKeystorePasswordField, cc.xy(3, 7, CellConstraints.FILL, CellConstraints.DEFAULT));
    }

    /**
     * @noinspection ALL
     */
    public JComponent $$$getRootComponent$$$() {
        return rootPanel;
    }

}
